package backend

import (
	"blog/models"
	"github.com/astaxie/beego/orm"
)

type BlogController struct {
	baseController
}

func (self *BlogController) Index() {
	self.display()
}

func (self *BlogController) Create() {
	category, _, _ := models.GetAllData(models.CategoryTable(), orm.NewCondition().And("deleted", 0))
	self.Data["category"] = category
	self.display()
}

func (self *BlogController) Edit() {
	id, _ := self.GetInt64("id", -1)
	if id == -1 {
		self.notFound("该文章不存在")
	}
	if !models.Exist(models.BlogTable(), orm.NewCondition().And("id", id)) {
		self.notFound("该文章不存在")
	}
	category, _, _ := models.GetAllData(models.CategoryTable(), orm.NewCondition().And("deleted", 0))
	self.Data["category"] = category
	blogDetail, _ := models.GetById(models.BlogTable(), int(id))
	self.Data["model"] = blogDetail
	self.display()
}
